//
//  EpointStringUtil.m
//  EpointFrame5
//  String Help
//  Created by 李亚春 on 13-12-16.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "EpointStringUtil.h"

@implementation EpointStringUtil

+(NSString *)getXMLFormatedAttribute:(NSString *)bs :(NSString *)xmltag
{
    NSString *tagq = [NSString stringWithFormat:@"<%@>",xmltag];
    NSString *tagh = [NSString stringWithFormat:@"</%@>",xmltag];
    NSRange srangeq = [bs rangeOfString:tagq];
    NSRange srangeh = [bs rangeOfString:tagh];
    if(srangeq.length>0&&srangeh.length>0){
        NSInteger loc = srangeq.location+srangeq.length;
        NSInteger len = srangeh.location-srangeq.location-srangeq.length;
        NSRange range = NSMakeRange(loc, len);
        NSString *des = [[NSString alloc]init];
        des = [bs substringWithRange:range];
        return des;
    }
    else
    {
        return @"";
    }
}

+(NSString *)filterXmlString:(NSString *)origionStr
{
    return [[[[[origionStr stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"]stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"]stringByReplacingOccurrencesOfString:@"'" withString:@"&apos;"]stringByReplacingOccurrencesOfString:@"\"" withString:@"&quot;"];
}

+(NSString *)getXMLFormatedAttributeBsWithTag:(NSString *)bs xmlTag:(NSString *)xmltag
{
    NSString *returnStr = @"";
    @try {
        NSString *tagq = [NSString stringWithFormat:@"<%@>",xmltag];
        NSString *tagh = [NSString stringWithFormat:@"</%@>",xmltag];
        NSRange srangeq = [bs rangeOfString:tagq];
        NSRange srangeh = [bs rangeOfString:tagh];
        if(srangeq.length>0&&srangeh.length>0){
            NSInteger loc = srangeq.location+srangeq.length;
            NSInteger len = srangeh.location-srangeq.location-srangeq.length;
            NSRange range = NSMakeRange(loc, len);
            NSString *des = [bs substringWithRange:range];
            returnStr = [NSString stringWithFormat:@"<%@>%@</%@>",xmltag,des,xmltag];
        }
    }
    @catch (NSException *exception) {
        returnStr = @"";
    }
    return returnStr;
}

+ (BOOL)stringIsNotNull:(NSString *)string {
    if (string && ![@"" isEqualToString:string]) {
        return YES;
    }
    return NO;
}


+(BOOL)startWith:(NSString*)prefix forString:(NSString*)text;
{
    if ( text != nil && prefix != nil ){
        if ( [prefix length] > [text length] ) {
            return NO;
        }
        
        NSString* prestr = [text substringToIndex:[prefix length]];
        if ( [self stringEquals:prestr to:prefix] ) {
            return YES;
        }
        
    }
    return NO;
}

+(BOOL)stringEquals:(NSString*)str1 to:(NSString*)str2
{
    if ( str1 == nil || str2 == nil ) {
        return NO;
    }
    return [str1 compare:str2 options:NSCaseInsensitiveSearch] == NSOrderedSame;
}

+(BOOL)endWith:(NSString*)suffix forString:(NSString*)text
{
    if ( text != nil && suffix != nil ){
        if ( [suffix length] > [text length] ) {
            return NO;
        }
        NSInteger len = [text length] - [suffix length];
        NSString* sufstr = [text substringFromIndex:len];
        if ( [self caseEquals:sufstr to:suffix] ) {
            return YES;
        }
        
    }
    return NO;
}

+(BOOL)caseEquals:(NSString*)str1 to:(NSString*)str2
{
    return (str1 == nil || str2 == nil) ? NO : [str1 isEqualToString:str2];
}

+(NSString *)gen_uuid
{
    CFUUIDRef uuidObject = CFUUIDCreate(kCFAllocatorDefault);
    
    NSString *uuidStr = (NSString *)CFBridgingRelease(CFUUIDCreateString(kCFAllocatorDefault, uuidObject));
    
    CFRelease(uuidObject);
    
    return uuidStr;
}

+ (NSString *)URLEncodedString:(NSString *)url
{
    NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                  (CFStringRef)url,
                                                                                  (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                                                  NULL,
                                                                                  kCFStringEncodingUTF8));
    return encodedString;
}


@end
